package com.thread_pool.thread_pool_executor.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author linjy on 2022/11/30
 */
@Configuration
public class ThreadConfig {

    @Bean(name = "traceThreadPool")
    public ThreadPoolExecutor threadPoolExecutor() {
        return new ThreadPoolExecutor(
                14,
                21,
                10,
                TimeUnit.MINUTES,
                //如果队列已经是满的，则创建一个新线程，如果线程数量已经达到maxPoolSize，则会执行拒绝策略
                new ArrayBlockingQueue<>(1000),
                new ThreadFactoryBuilder().setNameFormat("trace-thread-%d").build(),
                //该策略下，在调用者线程中直接执行被拒绝任务的run方法，除非线程池已经shutdown，则直接抛弃任务。
                new ThreadPoolExecutor.CallerRunsPolicy());
    }


}
